/**
 * Created by CJB on 2017-02-08 15:30:04.
 */
isPhoneValidator();//验证固话或者手机
strLengthValidator();//验证字符长度
var loadDiv = null;
$(function () {
	loadDiv = $("#loadDiv").loading();//初始化提示操作执行中
	    initAddWindow();
	    //去除所有控件的虚线框
		removeBlur();
	});
	
	/**
	 * 初始化添加窗口
	 */
	function initAddWindow(){
	    var zTree = $.fn.zTree.getZTreeObj('parentMenuTree');
	    zTree = zTree.setting.callback.onClick = onDialogTreeOnClick("departmentAddList","parentId");
	    //声明消息提示
	    var toolTips = new Object();
	    //设置表单验证
	    var validator = $("#addForm").validate({
	        rules:{
	        	name:{
	        		required:true,
	        		strMaxLenth:255,
	        	},
	            principal:{
	            	required:true,
	            	strMaxLenth:255,
	            },
	            phone:{
	        		required:true,
	        		isPhone:true
	        	},
	            departmentAddList:{
	            	required:true,
	            },
	            remark:{
	            	strMaxLenth:1000,
	            },
	            code:{
	            	strMaxLenth:255,
	            }
	        },
	        messages:{
	           
	        },
	        errorPlacement: function(eror,element){
	            var toolTip = createTooltip(toolTips,element.attr("id"));
	            toolTip.setContent(eror.text());
	            toolTip.close();
	            element.addClass("error-border");

	        },
	        success:function(label){
	            var elementId = label.attr("id").substring(0,label.attr("id").length - 6);
	            var toolTip = toolTips[elementId];
	            $("#" + elementId).removeClass("error-border");
	            toolTip.close();
	            toolTip.detach();
	        },
	        submitHandler:function(form){
	        	loadDiv.showLoading();
	            post(getPath() + "org/department/add",
	                $("#addForm").serialize(),
	                function(resultObject){
	            	loadDiv.hideLoading();
	                    var errors = resultObject.errors;
	                    //判断后台是否有返回错误消息
	                    if(errors && errors.length > 0){
	                        for(var i=0; i < errors.length; i++){
	                            var error = errors[i];
	                            var toolTip = createTooltip(toolTips,error.field);
	                            toolTip.setContent(error.defaultMessage);
	                            $("#" + error.field).addClass("error-border");
	                        }
	                    }else{
//	                        var item = resultObject.item;
//	                        var parentNode = getNodeById('parentMenuTree', item.parentId);
//	                        var menuparentNode = getNodeById('menuTree', item.parentId);
//	                        if(parentNode){
//	                            var newNode = {};
//	                            newNode.id = item.id;
//	                            newNode.pId = item.parentId;
//	                            newNode.name = item.name;
//	                            newNode.title = item.name;
//	                            var zTree = $.fn.zTree.getZTreeObj('parentMenuTree');
//	                            zTree.addNodes(parentNode, newNode);
//	                            var zTree = $.fn.zTree.getZTreeObj('menuTree');
//	                            zTree.addNodes(menuparentNode, newNode);
//	                            zTree.expandNode(menuparentNode,true);
//	                        }
//	                        $("#content").html("");
//	                        //添加成功，弹出添加成功提示框，当用户点击确定后刷新页面
//	                        createWindow("添加成功").dialog("open");
	                    	/*createWindow("添加成功",{url: getPath() + "org/department/list"}).dialog("open");*/
	                    	createWindow("添加成功");
	                    	$(".grid-list").load(getPath() + "org/department/add/0");
	                    }
	                },function(resultObject){
	                	createWindow("添加成功");
	                	loadDiv.hideLoading();
	                	//createWindow("添加成功");
		        		createWindow(resultObject).dialog('open');
		        	}
	            )
	        }
	    });
	    /*//隐藏列表，显示添加
		$(".grid-list").show();
		$('.grid-list').hide();
		//取消
		$('#cancleBtn').click(function(){
			$(".grid-list").hide();
			$(".grid-list").load(getPath() + "org/department/add/0");
		});*/
	    $("#departmentAddList").click({menuContentId:"parentMenu"}, showMenu);
}

	
	
	
	function heavyLoad(){
		$(".grid-list").load(getPath() + "org/department/add/0");
	}